package cn.com.lsy.product.lepao.controller;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.controller.base.BaseController;
import cn.com.lsy.product.lepao.model.entities.*;
import cn.com.lsy.product.lepao.request.*;
import cn.com.lsy.product.lepao.response.*;
import cn.com.lsy.product.lepao.response.base.ApiResponseEnum;
import cn.com.lsy.product.lepao.response.base.BaseRet;
import cn.com.lsy.product.lepao.response.base.PageInfo;
import cn.com.lsy.product.lepao.service.IAccessoryService;
import cn.com.lsy.product.lepao.service.IJpushService;
import cn.com.lsy.product.lepao.service.IStoreService;
import cn.com.lsy.product.lepao.service.ISystemService;
import cn.com.lsy.product.lepao.util.CommonFunctionUtil;
import cn.com.lsy.product.lepao.util.DozerMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(value = "系统管理接口", tags = "系统管理接口")
@RestController
@RequestMapping(value = "/lepao/console/doAuth")
public class SystemController extends BaseController {

    private Logger log = Logger.getLogger(SystemController.class);

    @Autowired
    ISystemService iSystemService;
    @Autowired
    IAccessoryService iAccessoryService;
    @Autowired
    IStoreService iStoreService;
    @Autowired
    IJpushService iJpushService;

    @RequestMapping(value = "/bannerList", method = RequestMethod.POST)
    @ApiOperation(notes = "轮播图列表", httpMethod = "POST", value = "轮播图列表")
    public BaseRet<List<BannerListRet>> bannerList() {

        BaseRet<List<BannerListRet>> ret = new BaseRet<List<BannerListRet>>();

        try {

            List<LpBannerTbl> list = iSystemService.selectBannerList();

            List<BannerListRet> listRet = DozerMapperUtil.mapList(list, BannerListRet.class);

            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setData(listRet);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/bannerInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "轮播图详情", httpMethod = "POST", value = "轮播图详情")
    public BaseRet<BannerInfoRet> bannerInfo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param, BindingResult result) {

        BaseRet<BannerInfoRet> ret = new BaseRet<BannerInfoRet>();

        try {

            checkParam(result);

            LpBannerTbl info = iSystemService.selectBannerInfo(param.getId());

            BannerInfoRet infoRet = DozerMapperUtil.map(info, BannerInfoRet.class);

            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setData(infoRet);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateBanner", method = RequestMethod.POST)
    @ApiOperation(notes = "修改轮播图", httpMethod = "POST", value = "修改轮播图")
    public BaseRet<String> updateBanner(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateBannerParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpBannerTbl lpBannerTbl = DozerMapperUtil.map(param, LpBannerTbl.class);


            if (iSystemService.updateBanner(lpBannerTbl)) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("修改成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("修改失败");
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addBanner", method = RequestMethod.POST)
    @ApiOperation(notes = "添加轮播图", httpMethod = "POST", value = "添加轮播图")
    public BaseRet<String> addBanner(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddBannerParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpBannerTbl lpBannerTbl = DozerMapperUtil.map(param, LpBannerTbl.class);
            lpBannerTbl.setCreateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));

            if (iSystemService.insertBanner(lpBannerTbl)) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("添加成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("添加失败");
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/delBanner", method = RequestMethod.POST)
    @ApiOperation(notes = "删除轮播图", httpMethod = "POST", value = "删除轮播图")
    public BaseRet<String> delBanner(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            if (iSystemService.delBanner(param.getId())) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("删除成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("删除失败");
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/versionList", method = RequestMethod.POST)
    @ApiOperation(notes = "版本列表", httpMethod = "POST", value = "版本列表")
    public BaseRet<List<VersionListRet>> versionList() {

        BaseRet<List<VersionListRet>> ret = new BaseRet<List<VersionListRet>>();

        try {

            List<LpVersionTbl> list = iSystemService.selectVersionList();

            List<VersionListRet> listRet = DozerMapperUtil.mapList(list, VersionListRet.class);

            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setData(listRet);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addVersion", method = RequestMethod.POST)
    @ApiOperation(notes = "添加版本", httpMethod = "POST", value = "添加版本")
    public BaseRet<String> addVersion(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddVersionParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpVersionTbl lpVersionTbl = DozerMapperUtil.map(param, LpVersionTbl.class);
            lpVersionTbl.setAddTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));

            if (iSystemService.insertVersion(lpVersionTbl)) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("添加成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("添加失败");
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/delVersion", method = RequestMethod.POST)
    @ApiOperation(notes = "删除版本", httpMethod = "POST", value = "删除版本")
    public BaseRet<String> delVersion(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            if (iSystemService.delVersion(param.getId())) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("删除成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("删除失败");
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/goodsList", method = RequestMethod.POST)
    @ApiOperation(notes = "商品列表", httpMethod = "POST", value = "商品列表")
    public BaseRet<PageInfo<List<GoodsListRet>>> goodsList(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody GoodsListParam param, BindingResult result) {

        BaseRet<PageInfo<List<GoodsListRet>>> ret = new BaseRet<PageInfo<List<GoodsListRet>>>();

        PageInfo<List<GoodsListRet>> pageInfo = new PageInfo<List<GoodsListRet>>();

        try {

            checkParam(result);
            int count = iSystemService.selectGoodsByCount(param.getType(), param.getSearchCondition());
            pageInfo.setCount(count);
            List<LpGoodsTbl> list = iSystemService.selectGoodsByPage(param.getStart(), param.getPageSize(), param.getType(), param.getSearchCondition());

            List<GoodsListRet> listRet = new ArrayList<GoodsListRet>();

            if (list != null && list.size() > 0) {

                listRet = DozerMapperUtil.mapList(list, GoodsListRet.class);
            }

            pageInfo.setRecords(listRet);

            ret.setData(pageInfo);
            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/goodsInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "商品详情", httpMethod = "POST", value = "商品详情")
    public BaseRet<GoodsInfoRet> goodsInfo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param, BindingResult result) {

        BaseRet<GoodsInfoRet> ret = new BaseRet<GoodsInfoRet>();

        try {

            checkParam(result);

            LpGoodsTblWithBLOBs info = iSystemService.selectGoodsInfo(param.getId());

            GoodsInfoRet infoRet = DozerMapperUtil.map(info, GoodsInfoRet.class);

            ret.setData(infoRet);
            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateGoods", method = RequestMethod.POST)
    @ApiOperation(notes = "修改商品", httpMethod = "POST", value = "修改商品")
    public BaseRet<String> updateGoods(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateGoodsParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            String startTime = param.getStartTime();
            String endTime = param.getEndTime();

            param.setStartTime(null);
            param.setEndTime(null);

            LpGoodsTblWithBLOBs lpGoodsTbl = DozerMapperUtil.map(param, LpGoodsTblWithBLOBs.class);
            lpGoodsTbl.setUpdateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            if (StringUtils.isNotEmpty(startTime)) {
                lpGoodsTbl.setStartTime(CommonFunctionUtil.changeTimeDate(startTime, Constant.formatType.second));
            }
            if (StringUtils.isNotEmpty(endTime)) {
                lpGoodsTbl.setEndTime(CommonFunctionUtil.changeTimeDate(endTime, Constant.formatType.second));
            }

            if (iSystemService.updateGoods(lpGoodsTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addGoods", method = RequestMethod.POST)
    @ApiOperation(notes = "添加商品", httpMethod = "POST", value = "添加商品")
    public BaseRet<String> addGoods(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddGoodsParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            String startTime = param.getStartTime();
            String endTime = param.getEndTime();

            param.setStartTime(null);
            param.setEndTime(null);

            LpGoodsTblWithBLOBs lpGoodsTbl = DozerMapperUtil.map(param, LpGoodsTblWithBLOBs.class);
            lpGoodsTbl.setInsertTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            lpGoodsTbl.setState(Integer.valueOf(Constant.status.enable));

            if (StringUtils.isNotEmpty(startTime)) {
                lpGoodsTbl.setStartTime(CommonFunctionUtil.changeTimeDate(startTime, Constant.formatType.second));
            }
            if (StringUtils.isNotEmpty(endTime)) {
                lpGoodsTbl.setEndTime(CommonFunctionUtil.changeTimeDate(endTime, Constant.formatType.second));
            }

            if (iSystemService.insertGoods(lpGoodsTbl)) {

                ret.setMsg("添加成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("添加失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/delGoods", method = RequestMethod.POST)
    @ApiOperation(notes = "删除商品", httpMethod = "POST", value = "删除商品")
    public BaseRet<String> delGoods(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            if (iSystemService.delGoods(param.getId())) {

                ret.setMsg("删除成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("删除失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/getGoodsStore", method = RequestMethod.POST)
    @ApiOperation(notes = "获取商品门店列表", httpMethod = "POST", value = "获取商品门店列表")
    public BaseRet<GetGoodsStoreRet> getGoodsStore() {

        BaseRet<GetGoodsStoreRet> ret = new BaseRet<GetGoodsStoreRet>();

        try {

            GetGoodsStoreRet getGoodsStoreRet = new GetGoodsStoreRet();
            List<StoreNameListRet> storeList = new ArrayList<StoreNameListRet>();
            List<StoreNameListRet> goodsList = new ArrayList<StoreNameListRet>();

            List<LpStoreTbl> store = iStoreService.selectByPage(0, 1000, "", null);
            List<LpGoodsTbl> goods = iSystemService.selectGoodsByPage(0, 1000, "", "");

            for (LpStoreTbl value : store) {
                StoreNameListRet storeNameListRet = new StoreNameListRet();
                storeNameListRet.setId(value.getId());
                storeNameListRet.setName(value.getStoreName());
                storeList.add(storeNameListRet);
            }

            for (LpGoodsTbl value : goods) {
                StoreNameListRet storeNameListRet = new StoreNameListRet();
                storeNameListRet.setId(value.getId());
                storeNameListRet.setName(value.getGoodsName());
                goodsList.add(storeNameListRet);
            }

            getGoodsStoreRet.setGoodsList(goodsList);
            getGoodsStoreRet.setStoreList(storeList);

            ret.setData(getGoodsStoreRet);
            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }
}
